<?php

class MyStatus
{

    /**
    *   Gets status values as array from app.yml, under section app_status_SECTION-YOU-NEED
    *   @param $section string - section name under status, in app.yml
    *   @return array of status values
    */
    public static function getStatusList($section)
    {
        $configName = 'app_status_' . $section;
        $ar = sfConfig::get($configName);
        if( empty($ar) )
        {
            $ar = array();
        }

        return $ar;
    }

    /**
    *   Returns array of named status (taken from app.yml) values from string
    *   @param $strValues string - status values as string, i.e. 1:4:5:
    *   @param $section string section name for status values in app.yml
    *   @param $strict boolean - set to false if status in string could be incorrect
    *   @return array of status values with names
    */
    public static function splitToNames($strValues, $section, $strict = true)
    {
        if( !$strValues )
            return array();

        // Status values which are in string
        $arSelected = preg_split('/:/', $strValues, -1, PREG_SPLIT_NO_EMPTY);
        //debugvar($arSelected, 'Status to split:');

        // Get status values from config
        $arConfigStatus = MyStatus::getStatusList($section);

        // Return empty array if no values in app.yml
        if( !count($arConfigStatus) )
            return $strict ? array() : $arSelected;

        $arStatus = array();
        // Loop selected status values, validate and connect with status name
        foreach( $arSelected as $i => $value )
        {
            if( !empty( $arConfigStatus[$value] ) )
                $arStatus[$value] = $arConfigStatus[$value];
            elseif( !$strict )
            {
                // Add incorrect status only if this is allowed
                $arStatus[$value] = $value;
            }
        }

        return $arStatus;
    }

    /**
    *   Joins selected statuses to sting. Performs check of non-empty array
    *   @param $ar array - array of selected status values
    *   @return string of selected status values or empty string
    */
    public static function join( $ar )
    {
        if( empty( $ar ) || !is_array($ar) )
            return '';

        return join(':', $ar);
    }
}